//
//  YEMSlideView.m
//  App_YueEnjoyMusic
//
//  Created by brother on 16/3/2.
//  Copyright © 2016年 brother. All rights reserved.
//

#import "YEMSlideView.h"


#define UIColorRGBA(r, g, b, a) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]

#define kWindowW  ([[UIScreen mainScreen] bounds].size.width)

#define kWindowH  ([[UIScreen mainScreen] bounds].size.height)

#define kTopViewHight 40

#define kZhuangTaiLanH 20


@interface YEMSlideView() <UIScrollViewDelegate>

/**  整个视图的大小 */
@property (nonatomic,assign) CGRect myViewFrame;

/**  上部分的ScrollView */
@property (nonatomic, strong) UIScrollView *topScrollView;

/**  下部分的ScrollView */
@property (nonatomic, strong) UIScrollView *downScrollView;

/**  上部分的View */
@property (nonatomic, strong) UIView *topView;

/**  上部分的按钮下面的滑动小view */
@property (nonatomic, strong) UIView *slideView;

/**  存放按钮的数组 */
@property (nonatomic, strong) NSMutableArray *topBtnMutArray;

@end

@implementation YEMSlideView


/**  设置自定义的frame */
- (instancetype)initWithFrame:(CGRect)frame WithviewControllerCountArray:(NSArray *)viewControllerCountArray {

    if (self = [super initWithFrame:frame]) {
        
        self.viewControllerCountArray = viewControllerCountArray;
        
        _myViewFrame = frame;
        
        
    }
    return self;
}


/**  存放控制器的数组 */
- (void)setViewControllerCountArray:(NSArray *)viewControllerCountArray {

    _viewControllerCountArray = viewControllerCountArray;
    
    _topBtnMutArray = [NSMutableArray array];
    
    /**  设置上面的视图 */
    [self setUpTopView];
    
    /**  设置下面的视图 */
    [self setUpDownView];
}


- (void)setUpTopView {

    // 设置按钮的宽度和高度
    CGFloat topBtnW = kWindowW / self.viewControllerCountArray.count;
    
    CGFloat topBtnH = kTopViewHight - 5;
    
    
    // 设置上面的视图的frame
    CGRect topViewFrame = CGRectMake(0, kZhuangTaiLanH, kWindowW, kTopViewHight);
    
    self.topView = [[UIView alloc]initWithFrame:topViewFrame];
    
    self.topView.backgroundColor = [UIColor yellowColor];
    
    [self addSubview:self.topView];
    
    
    // 下面滑动的小条条
    self.slideView = [[UIView alloc]initWithFrame:CGRectMake(0, kTopViewHight - 3,topBtnW, 3)];
    
    [self.slideView setBackgroundColor:[UIColor redColor]];
    
    [self.topView addSubview:self.slideView];
    
    
    // 设置按钮
    for (int i = 0; i < self.viewControllerCountArray.count; i++) {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * topBtnW, 0, topBtnW, topBtnH)];
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, topBtnW, topBtnH)];
        
        btn.tag = i;
        
        NSString *btnTitle = [self.viewControllerCountArray[i] allKeys][0];
//        NSLog(@"%@",btnTitle);
        
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:17];
        
        [btn setTitleColor:UIColorRGBA(52, 52, 52, 1) forState:UIControlStateNormal];
        
        if (i == 0) {
            
            [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
            
        }
        
        [btn addTarget:self action:@selector(tabButton:) forControlEvents:UIControlEventTouchUpInside];
        
        [view addSubview:btn];
        
        [self.topBtnMutArray addObject:btn];
        
        [self.topView addSubview:view];
    }
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0 ,kTopViewHight - 2, kWindowW,2)];
    
    lineView.backgroundColor = [UIColor greenColor];
    
    [self.topView addSubview:lineView];
    
    
}


- (void)tabButton:(id)sender {

    UIButton *button = sender;
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
    for (UIButton * btn in self.topBtnMutArray) {
    
        if (button != btn ) {
            
            [btn setTitleColor:UIColorRGBA(52, 52, 52, 1) forState:UIControlStateNormal];
            
        }
    }
    [self.downScrollView setContentOffset:CGPointMake(button.tag * kWindowW, 0) animated:YES];

}





- (void)setUpDownView {

    CGRect downScrollViewFrame = CGRectMake(0, kTopViewHight+kZhuangTaiLanH, kWindowW, kWindowH - kTopViewHight-kZhuangTaiLanH);
    
    self.downScrollView = [[UIScrollView alloc]initWithFrame:downScrollViewFrame];
    
    [self addSubview:self.downScrollView];
    
    for (int i = 0; i < self.viewControllerCountArray.count; i++) {
        
        CGRect  VCFrame = CGRectMake(i * kWindowW ,0,kWindowW,downScrollViewFrame.size.height);
        
        NSString *key = [self.viewControllerCountArray[i] allKeys][0];
        
        UIViewController *tempViewContorller = self.viewControllerCountArray[i][key];
        
        
        tempViewContorller.view.frame = VCFrame;
        
        [self.downScrollView addSubview:tempViewContorller.view];
    }
    
    self.downScrollView.contentSize = CGSizeMake(self.viewControllerCountArray.count * kWindowW, 0);
    // 启用分页
    self.downScrollView.pagingEnabled = YES;
    // 显示滑动的指标
    self.downScrollView.showsHorizontalScrollIndicator = NO;
    self.downScrollView.showsVerticalScrollIndicator = NO;
    // 定向锁定
    self.downScrollView.directionalLockEnabled = YES;
    // 不给反弹
    self.downScrollView.bounces = NO;
    self.downScrollView.delegate = self;
    
}

// 任何抵消变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGRect frame = self.slideView.frame;
    
    frame.origin.x = scrollView.contentOffset.x / self.viewControllerCountArray.count;
    
    self.slideView.frame = frame;
    
    int pageNumber = scrollView.contentOffset.x / kWindowW;
    
    for (UIButton * btn in self.topBtnMutArray) {
        
        if (btn.tag == pageNumber ) {
            
            [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
            
        }else{
            
            [btn setTitleColor:UIColorRGBA(52, 52, 52, 1) forState:UIControlStateNormal];
            
        }
        
    }
    
    
}























@end
